package com.fzh.wangxiao.web.dispatch.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fzh.wangxiao.utils.Result;
import com.fzh.wangxiao.web.dispatch.entity.TBizDispatch;
import com.fzh.wangxiao.web.dispatch.entity.TBizDispatchRecord;
import com.fzh.wangxiao.web.dispatch.mapper.TBizDispatchMapper;
import com.fzh.wangxiao.web.dispatch.service.TBizDispatchService;
import com.fzh.wangxiao.web.member.entity.TBizMember;
import com.fzh.wangxiao.web.order.entity.TBizOrder;
import com.fzh.wangxiao.web.order.service.TBizOrderService;
import com.fzh.wangxiao.web.req.DispatchListRequest;
import com.fzh.wangxiao.web.req.DispatchRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-01-05
 */
@Service
public class TBizDispatchServiceImpl extends ServiceImpl<TBizDispatchMapper, TBizDispatch> implements TBizDispatchService {



    @Autowired
    private TBizOrderService orderService;

    @Override
    public Result list(DispatchListRequest request) {
        Result result = new Result();
        Map<String,Object> map = new HashMap<>();
        map.put("userid",request.getUserId());
        map.put("pageno",(request.getPageNo()-1)*request.getPageSize());
        map.put("pagesize",request.getPageSize());
        List<TBizMember> list = baseMapper.getMembers(map);
        return result.setData(list);
    }

    @Override
    public Result add(TBizDispatch dispatch) {
        Result result = new Result();
        baseMapper.insert(dispatch);
        return result;
    }

    @Override
    public Result update(DispatchRequest request) {
        Result result = new Result();
        TBizDispatch tBizDispatch = new TBizDispatch();
        tBizDispatch.setMemberId(request.getMemberId());
        tBizDispatch.setUserId(request.getUserId());
        tBizDispatch.setId(request.getId());
        tBizDispatch.setStatus(request.getStatus());
        baseMapper.updateById(tBizDispatch);

        TBizDispatchRecord record = new TBizDispatchRecord();
        record.setRemarks(record.getRemarks());
        record.setDispatchId(request.getId());
        record.setMemberId(record.getMemberId());
        record.setUserId(record.getUserId());


        if(request.getStatus()==8){
            //插入订单
            TBizOrder order = new TBizOrder();
            order.setMemberId(request.getMemberId());
            order.setUserId(request.getUserId());
            order.setStatus(0);
            orderService.insert(order);
        }

        return result;
    }

}
